<?php


class WacDispatchOrderItemTable extends WacCommonTable
{
    
    public static function getInstance()
    {
        return Doctrine_Core::getTable('WacDispatchOrderItem');
    }

    /*
     * does the order and specified goods exist or not
     * @return boolean
     */
    public function existOrderGoods($order, $goods)
    {
        $orderId = is_object($order) ? $order->getId() : $order;
        $goodsId = is_object($goods) ? $goods->getId() : $goods;
        $objQuery = $this->createQuery('t1')
                ->select("count(*) as total")
                ->from("WacDispatchOrderItem t1")
                ->where("t1.dispatch_order_id=? and t1.goods_id", array($orderId, $goodsId));
        $dataResult = $objQuery->fetchOne();
        $objQuery->free();
        return ($dataResult['total']>0);
    }

    public function findOneByOrderAndGoods($order, $goods, $isArr=false)
    {
        $orderId = is_object($order) ? $order->getId() : $order;
        $goodsId = is_object($goods) ? $goods->getId() : $goods;
        
        $objQuery = $this->createQuery('t1')
                ->select('t1.*')
                ->from("WacDispatchOrderItem t1")
                ->where("t1.dispatch_order_id=? and t1.goods_id=?", array($orderId, $goodsId));

        if($isArr)
        {
            $dataResult = $objQuery->fetchOne(array(), Doctrine::HYDRATE_ARRAY);
        }
        else{
            $dataResult = $objQuery->fetchOne();
        }

        $objQuery->free();
        return $dataResult;
    }

    /*
     * return a custome list
     */
    public function getCustomList($arrParams, $page=1, $maxPerPage=20, $isArr=true)
    {
        $objQuery = $this->createQuery('t1')
                ->select('t1.*')
                ->from("WacDispatchOrderItem t1")
                ->leftJoin("t1.WacDispatchOrder wdo")
//                ->leftJoin("t1.WacGoodsCategory wgc")
                ->leftJoin("t1.WacGoods wg");

        if(is_array($arrParams) && count($arrParams)>0)
        {
            QueryHelper::processOption($objQuery, $arrParams);  // dont set "offset" and "limit" option in it
        }

        $this->_pager = new Doctrine_Pager($objQuery, $page, $maxPerPage);
        if($isArr)
        {
            $items = $this->_pager->execute(array(), Doctrine::HYDRATE_ARRAY);
        }
        else
        {
            $items = $this->_pager->execute();
        }
        $objQuery->free();
        return $items;
    }
}